package com.ruoyi.manage.cms.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.manage.cms.domain.HbCase;
import com.ruoyi.manage.cms.service.IHbCaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StrUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.manage.cms.domain.vo.HbCaseVo;
import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * caseController
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@Api(tags="case控制器")
@RestController
@RequestMapping("/manage/case" )
public class HbCaseController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IHbCaseService hbCaseService;

    /**
     * 查询case列表
     */
    @ApiOperation(value = "查询case",notes = "case管理")
    @PreAuthorize("@ss.hasPermi('manage:case:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(HbCaseVo hbCase,QueryWrapper<HbCase> queryWrapper) {
        setQueryCondition(hbCase,queryWrapper);
        startPage();
        List<HbCase> list = hbCaseService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(HbCaseVo hbCase, QueryWrapper<HbCase> queryWrapper) {
        if(StrUtil.isNotBlank(hbCase.getName())){
            queryWrapper.like(TABLE_PREFIX+"name" ,hbCase.getName());
        }
        if(StrUtil.isNotBlank(hbCase.getCompany())){
            queryWrapper.eq(TABLE_PREFIX+"company" ,hbCase.getCompany());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出case列表
     */
    @PreAuthorize("@ss.hasPermi('manage:case:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, HbCaseVo hbCase,QueryWrapper<HbCase> queryWrapper) {
        if (CollUtil.isEmpty( hbCase.getIds())){
            setQueryCondition(hbCase,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",hbCase.getIds());
        }
        List<HbCase> list = hbCaseService.list(queryWrapper);
        ExcelUtil<HbCase> util = new ExcelUtil<HbCase>(HbCase. class);
        util.exportExcel(response,list, "case");
    }

    /**
     * 获取case详细信息
     */
    @ApiOperation(value = "获取case",notes = "case管理")
    @PreAuthorize("@ss.hasPermi('manage:case:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(hbCaseService.queryGetById(id));
    }

    /**
     * 新增case
     */
    @ApiOperation(value = "新增case",notes = "case管理")
    @PreAuthorize("@ss.hasPermi('manage:case:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody HbCase hbCase) {
        return toAjax(hbCaseService.addHbCase(hbCase) ? 1 : 0);
    }

    /**
     * 修改case
     */
    @ApiOperation(value = "修改case",notes = "case管理")
    @PreAuthorize("@ss.hasPermi('manage:case:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody HbCase hbCase) {
        return toAjax(hbCaseService.updateHbCase(hbCase) ? 1 : 0);
    }

    /**
     * 删除case
     */
    @ApiOperation(value = "删除case",notes = "case管理")
    @PreAuthorize("@ss.hasPermi('manage:case:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hbCaseService.deleteHbCaseByIds(ids) ? 1 : 0);
    }
}
